<?php

include_once 'DAO/FollowDAO.class.php';
include_once 'DAO/UserDAO.class.php';
include_once 'DAO/TripDAO.class.php';

class UserUtils {
    
    // Briskoume to id tou xrhsth
    static function getUserId(){
        if(isset($_GET['userId'])){
            $userId = $_GET['userId'];         
        }
        else{
            if(isset($_SESSION['login'])){
                if($_SESSION['login']){
                    $userId = $_SESSION['userId'];
                }
                else{
                    $userId = '';
                }
            }
            else{
                $userId = '';
            }
        }
        return $userId;
    }

    // Pairnoume to gravatar hash
    static function getGravatarHash($userId){
        $userDao = new UserDAO();
        $email = $userDao->retrieveEmail($userId);
        return md5(trim(strtolower($email)));
    }

    // Pairnoume pinaka me tous followers
    static function getFollowers($userId){
        $followDao = new FollowDAO();
        return $followDao->retrieveFollowers($userId);
    }

    // Pairnoume pinaka me tous followings
    static function getFollowings($userId){
        $followDao = new FollowDAO();
        return $followDao->retrieveFollowings($userId);
    }

    // Pairnoume ton arithmo twn followers
    static function getFollowersCount($userId){
        return count(self::getFollowers($userId));
    }

    // Pairnoume ton arithmo twn followings
    static function getFollowingsCount($userId){
        return count(self::getFollowings($userId));
    }

    // Epistrefei html me to koumpi gia follow/unfollow xrhsth
    static function getFollowButton($userId){
        if(isset($_SESSION['login'])){
            // Ean o xrhsths exei kanei login
            if(($_SESSION['login'])){
                // Ean to userId einai tou logged-in xrhsth, o xrhsths
                // blepei to profil tou opote den theloume koumpi
                if((String)$_SESSION['userId'] == (String)$userId)
                    return '';
                // alliws elegxoume thn sxesh follow tou logged-in xrhsth
                // me ton xrhsth tou opoiou blepei to profil
                else{
                    $followDao = new FollowDAO();
                    // O logged-in xrhsths exei kanei follow auton poy blepei
                    if($followDao->checkFollow($_SESSION['userId'], $userId)){
                        return "<a href='removefollow.php?followingId=$userId'> Unfollow </a>";
                    }
                    // O logged-in xrhsths den kanei follow ayton poy blepei
                    else{
                        return "<a href='addfollow.php?followingId=$userId'> Follow </a>";
                    }
                }
            }
            // An o xrhsths den exei kanei login 
            else{
                return '';
            }
        }
    }

    // Epistrefei thn trexousa topothesia tou xrhsth
    static function getCurrentLocation($userId){
        $userDao = new UserDAO();
        return $userDao->retrieveCurrentLocation($userId);
    }

    // Epistrefei ta taksidia poy exei kanei o xrhsths kata to parelthon
    static function getPastTrips($userId){
        $tripDao = new TripDAO();
        return $tripDao->retrievePastTrips($userId);
    }

    // Epistrefei ta mellontika taksidia tou xrhsth
    static function getFutureTrips($userId){
        $tripDao = new TripDAO();
        return $tripDao->retrieveFutureTrips($userId);
    }
}
?>
